package ducct.footprint.ui;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import ducct.footprint.R;
import ducct.footprint.entity.MyFootprint;
import ducct.footprint.location.LocationOverlay;
import ducct.footprint.utils.Global;

public class MyFootprintDetailActivity extends MapActivity {
	private MapView mapView;
	private LocationOverlay locationOverlay;
	private MapController mapController;
	private MyFootprint myFootprint;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_footprint_detail);
		
		myFootprint = (MyFootprint) getIntent().getExtras().get(Global.MY_FOOTPRINT);
		
		displayMap();
		displayFootprintInfo();
	}
	
	private void displayFootprintInfo() {
		TextView txtLocationName = (TextView) findViewById(R.id.txtLocationName);
		txtLocationName.setText(myFootprint.locationName);
		
		TextView txtTimestamp = (TextView) findViewById(R.id.txtTimestamp);
		txtTimestamp.setText(myFootprint.getTime());
		
		Button btnViewNote = (Button) findViewById(R.id.btnViewNote);
		btnViewNote.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showNoteDialog();
			}
		});
	}

	protected void showNoteDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(myFootprint.note)
			.setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.dismiss();
				}
			}
		);
		
		AlertDialog alert = builder.create();
		alert.setTitle("Footprint's note");
		alert.show();
	}

	private void displayMap() {
		// map view
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);

		// overlay
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		locationOverlay = new LocationOverlay();
		locationOverlay.point = new GeoPoint(
				(int) (myFootprint.lat * 1E6),(int) (myFootprint.lng * 1E6));
		listOfOverlays.add(locationOverlay);

		// controller
		mapController = mapView.getController();
		mapController.setZoom(16);
		
		mapController.setCenter(locationOverlay.point);
		mapView.invalidate();	// TODO is need to invalidate map?
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
